package com.hzqy.service.industry.medical;

import com.hzqy.persistence.dao.industry.SdGoodOrderDao;
import com.hzqy.web.vo.industry.medical.SdGoodOrderVo;
import com.hzqy.web.vo.industry.medical.SdUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SdGoodOrderService {
    protected final Logger LOG = LoggerFactory.getLogger(getClass());
    @Resource
    private SdGoodOrderDao sdGoodOrderDao;
    
    public Map<String, Object> addDictOrder(SdGoodOrderVo sdGoodOrderVo) {
        LOG.debug("进入addDictOrder:"+sdGoodOrderVo+" 23行");
        Map<String,Object> resultMap =new HashMap<>();
        List<SdGoodOrderVo> list1=sdGoodOrderDao.selectOrderByOrderId(sdGoodOrderVo);
        if (list1==null||list1.size()==0){
            if (sdGoodOrderVo.getF_sd_money()!=null&&sdGoodOrderVo.getF_sd_money()>=0){
                int i = sdGoodOrderDao.addDictOrder(sdGoodOrderVo);
                if (i<=0){
                    resultMap.put("success",false);
                    resultMap.put("message","添加订单信息失败");
                    LOG.debug("addDictOrder:"+resultMap.get("message")+" 36行");
                    return resultMap;
                }
//            }
//            if (sdGoodOrderVo.getF_sd_money()!=null&&sdGoodOrderVo.getF_sd_money()>=0){
                SdGoodOrderVo sdGoodOrderVo1=new SdGoodOrderVo();
                sdGoodOrderVo1.setF_sd_oid(sdGoodOrderVo.getF_sd_poid());
                List<SdGoodOrderVo> list = sdGoodOrderDao.selectOrdersByPoid(sdGoodOrderVo1);
                if (list==null||list.size()==0){
                    resultMap.put("success",false);
                    resultMap.put("message","没有此总订单");
                }else{
                    int tmoney=0;
                    for (int i1 = 0; i1 < list.size(); i1++) {
                        tmoney+=list.get(i1).getF_sd_money();
                    }
                    sdGoodOrderVo1.setF_sd_money(tmoney);
                    int i1 = sdGoodOrderDao.updateDictOrderTotal(sdGoodOrderVo1);
                    if (i1<=0){
                        resultMap.put("success",false);
                        resultMap.put("message","修改订单信息失败");
                        LOG.debug("addDictOrder:"+resultMap.get("message")+" 56行");
                        return resultMap;
                    }
                    resultMap.put("success",true);
                    resultMap.put("message","添加订单成功");
                }
            }else{
                resultMap.put("success",false);
                resultMap.put("message","请输入正确的单价");
            }
        }else{
            resultMap.put("success",false);
            resultMap.put("message","已经添加过该条数据");
            LOG.debug("addDictOrder:"+resultMap.get("message")+" 29行");
            return resultMap;
        }
        LOG.debug("addDictOrder:"+resultMap.get("message")+" 66行");
        return resultMap;
    }
    
    public Map<String, Object> addDictOrderTotal(SdGoodOrderVo sdGoodOrderVo) {
        LOG.debug("进入addDictOrderTotal:"+sdGoodOrderVo+" 71行");
        Map<String,Object> resultMap =new HashMap<>();
        int i= sdGoodOrderDao.addDictOrderTotal(sdGoodOrderVo);
        if (i==0){
            resultMap.put("success",false);
            resultMap.put("message","添加订单失败");
        }else {
            resultMap.put("success",true);
            resultMap.put("message","添加订单成功");
        }
        LOG.debug("addDictOrderTotal:"+resultMap.get("message")+" 81行");
        return resultMap;
    }

    public Map<String, Object> selectOrderByPoid(SdGoodOrderVo sdGoodOrderVo,Integer pageNum, Integer pageSize) {
        LOG.debug("进入selectOrderByPoid:"+sdGoodOrderVo+" 86行");
        Map<String,Object> resultMap =new HashMap<>();
        List<SdGoodOrderVo> sdGoodOrderVos=sdGoodOrderDao.selectOrderByPoidCount(sdGoodOrderVo);
        int rowCount=0;
        if (sdGoodOrderVos!=null){rowCount=sdGoodOrderVos.size();}
        resultMap.put("total",rowCount);
        if(rowCount>0 && pageNum>0 && pageSize>0){
            resultMap.put("firstResult", (pageNum-1)*pageSize);
            resultMap.put("maxResults", pageSize);
            resultMap.put("f_sd_oid",sdGoodOrderVo.getF_sd_oid());
            List<SdGoodOrderVo> list=sdGoodOrderDao.selectOrderByPoid(resultMap);
            resultMap.put("rows", list);
            resultMap.put("code","200");
            resultMap.put("message","查询数据成功");
        }else{
            resultMap.put("code","201");
            resultMap.put("message","查询数据失败");
            resultMap.put("rows", new ArrayList<SdUserVo>());
        }
        LOG.debug("selectOrderByPoid:"+resultMap.get("message")+" 105行");
        return resultMap;
    }

    public Map<String, Object> deleteOrderByTotal(SdGoodOrderVo sdGoodOrderVo) {
        LOG.debug("进入deleteOrderByTotal:"+sdGoodOrderVo+" 110行");
        Map<String,Object> resultMap =new HashMap<>();
        int i=sdGoodOrderDao.deleteOrderByTotal(sdGoodOrderVo);
        if (i<=0){
            resultMap.put("success",false);
            resultMap.put("result","删除订单失败");
        }else{
            resultMap.put("success",true);
            resultMap.put("result","删除订单成功");
        }
        LOG.debug("deleteOrderByTotal:"+resultMap.get("message")+" 120行");
        return resultMap;
    }

    public Map<String, Object> deleteOrderById(SdGoodOrderVo sdGoodOrderVo) {
        LOG.debug("进入deleteOrderById:"+sdGoodOrderVo+" 125行");
        Map<String,Object> resultMap =new HashMap<>();
        List<SdGoodOrderVo> list = sdGoodOrderDao.selectTotalOrderByIdTime(sdGoodOrderVo);
        if (list==null||list.size()==0){
            resultMap.put("success",false);
            resultMap.put("result","删除订单失败");
            LOG.debug("deleteOrderById:"+resultMap.get("message")+" 131行");
            return resultMap;
        }else{
            list.get(0).setF_sd_money(list.get(0).getF_sd_money()-sdGoodOrderVo.getF_sd_money());
            int i = sdGoodOrderDao.updateDictOrderTotal(list.get(0));
            if (i<=0){
                resultMap.put("success",false);
                resultMap.put("result","删除订单失败");
                LOG.debug("deleteOrderById:"+resultMap.get("message")+" 138行");
                return resultMap;
            }
        }
        int i=sdGoodOrderDao.deleteOrderById(sdGoodOrderVo);
        if (i<=0){
            resultMap.put("success",false);
            resultMap.put("result","删除订单失败");
        }else{
            resultMap.put("success",true);
            resultMap.put("result","删除订单成功");
        }
        LOG.debug("deleteOrderById:"+resultMap.get("message")+" 151行");
        return resultMap;
    }

    public Map<String, Object> selectDictTotalOrder(SdUserVo sdUserVo,Integer pageNum, Integer pageSize) {
        LOG.debug("进入selectDictTotalOrder:"+sdUserVo+" 159行");
        Map<String,Object> resultMap =new HashMap<>();
        List<SdUserVo> sdUserVos = sdGoodOrderDao.selectDictOrderCount(sdUserVo);
        int rowCount=0;
        if (sdUserVos!=null){rowCount=sdUserVos.size();}
        resultMap.put("total",rowCount);
        if(rowCount>0 && pageNum>0 && pageSize>0){
            resultMap.put("f_sd_village",sdUserVo.getF_sd_village());
            resultMap.put("f_sd_atArea",sdUserVo.getF_sd_atArea());
            resultMap.put("f_sd_sex",sdUserVo.getF_sd_sex());
            resultMap.put("f_sd_surname",sdUserVo.getF_sd_surname());
            resultMap.put("f_sd_orderid",sdUserVo.getF_sd_orderid());
            resultMap.put("f_sd_buyTime",sdUserVo.getF_sd_buyTime());
            resultMap.put("firstResult", (pageNum-1)*pageSize);
            resultMap.put("maxResults", pageSize);
            List<SdUserVo> list=sdGoodOrderDao.selectDictTotalOrder(resultMap);
            resultMap.put("rows", list);
            resultMap.put("code","200");
            resultMap.put("message","查询数据成功");
        }else{
            resultMap.put("code","201");
            resultMap.put("message","查询数据失败");
            resultMap.put("rows", new ArrayList<SdUserVo>());
        }
        LOG.debug("selectDictTotalOrder:"+resultMap.get("message")+" 180行");
        return resultMap;
    }

    public Map<String, Object> updateOrder(SdGoodOrderVo sdGoodOrderVo) {
        LOG.debug("进入updateOrder:"+sdGoodOrderVo+" 85行");
        Map<String,Object> resultMap =new HashMap<>();
        int i=sdGoodOrderDao.updateOrder(sdGoodOrderVo);
        if (i<=0){
            resultMap.put("success",false);
            resultMap.put("message","修改订单信息失败");
            LOG.debug("updateOrder:"+resultMap.get("message")+" 191行");
            return resultMap;
        }else{
            resultMap.put("success",true);
            resultMap.put("message","修改订单信息成功");
        }
        if (sdGoodOrderVo.getF_sd_money()!=null){
            SdGoodOrderVo sdGoodOrderVo1=new SdGoodOrderVo();
            sdGoodOrderVo1.setF_sd_oid(sdGoodOrderVo.getF_sd_poid());
            List<SdGoodOrderVo> list = sdGoodOrderDao.selectOrdersByPoid(sdGoodOrderVo1);
            if (list==null||list.size()==0){
                resultMap.put("success",false);
                resultMap.put("message","修改订单信息失败");
            }else{
                int tmoney=0;
                for (int i1 = 0; i1 < list.size(); i1++) {
                    tmoney+=list.get(i1).getF_sd_money();
                }
                sdGoodOrderVo1.setF_sd_money(tmoney);
                int i1 = sdGoodOrderDao.updateDictOrderTotal(sdGoodOrderVo1);
                if (i1<=0){
                    resultMap.put("success",false);
                    resultMap.put("message","修改订单信息失败");
                    LOG.debug("updateOrder:"+resultMap.get("message")+" 214行");
                    return resultMap;
                }
            }
        }
        LOG.debug("updateOrder:"+resultMap.get("message")+" 219行");
        return resultMap;
    }

    public Map<String, Object> selectDictTotalOrderByChoose(SdUserVo sdUserVo, Integer pageNum, Integer pageSize) {
        LOG.debug("进入selectDictTotalOrderByChoose:"+sdUserVo+" 224行");
        Map<String,Object> resultMap =new HashMap<>();
        List<SdUserVo> sdUserVos = sdGoodOrderDao.selectOrderCountByChoose(sdUserVo);
        int rowCount=0;
        if (sdUserVos!=null){rowCount=sdUserVos.size();}
        resultMap.put("total",rowCount);
        if(rowCount>0 && pageNum>0 && pageSize>0){
            resultMap.put("f_sd_village",sdUserVo.getF_sd_village());
            resultMap.put("f_sd_atArea",sdUserVo.getF_sd_atArea());
            resultMap.put("f_sd_sex",sdUserVo.getF_sd_sex());
            resultMap.put("f_sd_surname",sdUserVo.getF_sd_surname());
            resultMap.put("f_sd_orderid",sdUserVo.getF_sd_orderid());
            resultMap.put("f_sd_buyTime",sdUserVo.getF_sd_buyTime());
            resultMap.put("firstResult", (pageNum-1)*pageSize);
            resultMap.put("maxResults", pageSize);
            List<SdUserVo> list=sdGoodOrderDao.selectDictTotalOrderByChoose(resultMap);
            resultMap.put("rows", list);
            resultMap.put("code","200");
            resultMap.put("message","查询数据成功");
        }else{
            resultMap.put("code","201");
            resultMap.put("message","查询数据失败");
            resultMap.put("rows", new ArrayList<SdUserVo>());
        }
        LOG.debug("selectDictTotalOrderByChoose:"+resultMap.get("message")+" 248行");
        return resultMap;
    }

    public SdGoodOrderVo selectOrderById(SdGoodOrderVo sdGoodOrderVo) {
        LOG.debug("进入addDictOrder:"+sdGoodOrderVo+" 253行");
        return sdGoodOrderDao.selectOrderById(sdGoodOrderVo);
    }
}
